package com.lihuacats.bilibili.task.bilibili_task.domain;

/**
 * @author : kurisu
 * @className : MessageVO
 * @description :
 * @date: 2021-10-18 20:11
 */
public class MessageVO<T> {
    public static final int SUCCESS_CODE = 0;
    public static final int FAILURE_CODE = -1;
    private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";
    private static final String DEFAULT_FAILURE_MESSAGE = "操作失败";
    private boolean isSuccess = true;
    private int code;
    private String result;
    private T data;


    public static <T> MessageVO<T> create(final boolean success,
                                          final int code,
                                          final String message,
                                          final T data) {
        final MessageVO<T> result = new MessageVO<>();
        result.setSuccess(success);
        result.setCode(code);
        result.setResult(message == null ? (success ? DEFAULT_SUCCESS_MESSAGE : DEFAULT_FAILURE_MESSAGE) : message);
        result.setData(data);
        return result;
    }

    public static <T> MessageVO<T> create(final boolean success,
                                          final int code,
                                          final String message) {
        final MessageVO<T> result = new MessageVO<>();
        result.setSuccess(success);
        result.setCode(code);
        result.setResult(message == null ? (success ? DEFAULT_SUCCESS_MESSAGE : DEFAULT_FAILURE_MESSAGE) : message);
        return result;
    }

    public static <T> MessageVO<T> success() {
        return success(null);
    }

    public static <T> MessageVO<T> success(T data) {
        return create(true, SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, data);
    }


    public static <T> MessageVO<T> failure() {
        return failure(DEFAULT_FAILURE_MESSAGE);
    }

    public static <T> MessageVO<T> failure(String msg) {
        return failure(FAILURE_CODE, msg);
    }

    public static <T> MessageVO<T> failure(int code, String msg) {
        return create(false, code, msg);
    }


    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
